package com.tanhua.dubbo.api;

import com.tanhua.domain.mongo.Friend;
import com.tanhua.domain.vo.PageResult;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@Service
public class FriendApiImpl implements FriendApi {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void addContact(Long userId1, Long userId2) {
        //1. 查询是否存在好友关系
        if (!isContacts(userId1, userId2)) {
            addFriend(userId1, userId2);
        }
        if (!isContacts(userId2, userId1)) {
            addFriend(userId2, userId1);
        }
    }

    @Override
    public PageResult findFriendList(Integer page, Integer pagesize, Long userId) {
        Query query = Query.query(Criteria.where("userId").is(userId));
        query.with(PageRequest.of(page - 1, pagesize));
        query.with(Sort.by(Sort.Order.desc("created")));
        List<Friend> friends = mongoTemplate.find(query, Friend.class);
        long count = mongoTemplate.count(query, Friend.class);

        int pages = (int) Math.ceil(count * 1.0 / pagesize);
        return new PageResult((int) count, pagesize, pages, page, friends);
    }
    //查询好友关系
    @Override
    public Friend query1(Long id, Long userId) {
        Query query = new Query();
        query.addCriteria(Criteria.where("userId").is(userId).and("friendId").is(id));
        return mongoTemplate.findOne(query,Friend.class);
    }
    //添加好友关系
    @Override
    public void insert(Friend friend1) {
        Query query = Query.query(Criteria.where("userId").is(friend1.getUserId()).and("friendId").is(friend1.getFriendId()));
        mongoTemplate.remove(query, Friend.class);

        query = Query.query(Criteria.where("userId").is(friend1.getFriendId()).and("friendId").is(friend1.getUserId()));
        mongoTemplate.remove(query, Friend.class);

    }

    //查询好友关系
    @Override
    public Friend queryy(Long id, Long userId) {
        Query query = new Query();
        query.addCriteria(Criteria.where("userId").is(userId).and("friendId").is(id));
        return mongoTemplate.findOne(query, Friend.class);
    }

    @Override
    public void removeFriend(Long id, Long userId) {
        Query query = Query.query(Criteria.where("userId").is(id).and("friendId").is(userId));
        mongoTemplate.remove(query, Friend.class);

        query = Query.query(Criteria.where("friendId").is(userId).and("userId").is(id));
        mongoTemplate.remove(query, Friend.class);
    }

    //存在返回true
    public boolean isContacts(Long userId1, Long userId2) {
        Query query = Query.query(Criteria.where("userId").is(userId1).and("friendId").is(userId2));
        return mongoTemplate.exists(query, Friend.class);
    }

    //添加好友关系
    public void addFriend(Long userId1, Long userId2) {
        Friend friend = new Friend();
        friend.setUserId(userId1);
        friend.setFriendId(userId2);
        friend.setCreated(System.currentTimeMillis());
        friend.setId(ObjectId.get());
        mongoTemplate.insert(friend);
    }
}
